import 'dart:async';

import 'package:dingtalk/core/events/connect_event.dart';
import 'package:dingtalk/core/utils/event_bus_util.dart';

import 'package:dingtalk/core/values/values.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';

import 'widgets/message_list_view.dart';
import 'widgets/message_popmenu.dart';
import 'widgets/message_search_decoration.dart';
import 'widgets/message_top_mask.dart';
import 'widgets/message_top_quick.dart';

class THMessageScreen extends StatefulWidget {
  @override
  _THMessageScreenState createState() => _THMessageScreenState();
}

class _THMessageScreenState extends State<THMessageScreen> with AutomaticKeepAliveClientMixin {
  ScrollController _scrollController;

  double _topQuickOpacity = 0.0;

  StreamSubscription _subscription;

  THSocketConnectType _reconnectStatus = THSocketConnectType.RECONNECTING;

  @override
  void initState() {
    super.initState();

    _scrollController = ScrollController();

    _scrollController
      ..addListener(() {
        int offset = _scrollController.offset.ceil();
        this.setState(() {
          _topQuickOpacity = offset > kSize70 ? 1.0 : 0.0;
        });
      });

    _subscription =
        EventBusUtil.getInstance().on<THSocketConnectEvent>().listen((event) {
          setState(() {
            _reconnectStatus = event.status;
          });
        });
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: buildAppBar(context),
      body: Stack(
        children: <Widget>[
          SingleChildScrollView(
            controller: _scrollController,
            child: Column(
              children: <Widget>[
                THMessageSearchDecoration(),
                THMessageTopQuick(),
                THMessageTopMask(),
                THMessageListView(),
              ],
            ),
          ),
          Positioned(
            top: 0,
            left: 0,
            right: 0,
            child: AnimatedOpacity(
                opacity: _topQuickOpacity,
                duration: Duration.zero,
                child:
                Container(color: Colors.white, child: THMessageTopQuick())),
          ),
        ],
      ),
    );
  }

  AppBar buildAppBar(BuildContext context) {
    return AppBar(
      automaticallyImplyLeading: false,
      title: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          GestureDetector(
            child: CircleAvatar(
              backgroundColor: kPrimaryColor,
              radius: kSize40,
              child: Text(
                "江景",
                style: TextStyle(color: Colors.white),
              ),
            ),
            onTap: () {
              Scaffold.of(context).openDrawer();
            },
          ),
          Padding(
            padding: EdgeInsets.only(left: kSize24),
            child: _reconnectStatus == THSocketConnectType.SUCCESS
                ? SizedBox()
                : _reconnectStatus == THSocketConnectType.RECONNECT_FAILED
                ? Text(
              "连接失败",
              style: TextStyle(color: kColor33, fontSize: kSize30),
            )
                : Row(
              children: [
                SpinKitRotatingCircle(
                  color: kTHPrimary,
                  size: kSize50,
                ),
                Text(
                  "正在连接...",
                  style:
                  TextStyle(color: kColor33, fontSize: kSize30),
                ),
              ],
            ),
          )
        ],
      ),
      actions: <Widget>[THMessagePopMenu()],
    );
  }

  @override
  void dispose() {
    if (null != _subscription) {
      _subscription.cancel();
    }
    super.dispose();
  }

  @override
  bool get wantKeepAlive => true;
}
